package com.mooc.yohoshop.ui.home;

import com.blankj.utilcode.util.Utils;
import com.mooc.yohoshop.ApiService;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.CategoryEntity;
import com.mooc.yohoshop.entity.ErroEntity;
import com.mooc.yohoshop.network.BaseRepository;
import com.mooc.yohoshop.network.HttpFactory;
import com.mooc.yohoshop.network.HttpType;
import java.util.HashMap;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

public class HomeRepo extends BaseRepository {

    //请求banner
    private Observable<BaseEntity>requestBanner(){
        return convertObservable(HttpFactory.factory(HttpType.UPLOADTYPE).getRetrofit()
                .create(ApiService.class).requestBanner());
    }
    //请求推荐商品
    public Observable<BaseEntity>requestGoods(HashMap map){
        return convertObservable(HttpFactory.factory(HttpType.SIGNTYPE).getRetrofit()
                .create(ApiService.class).requestHomeGoods(createBody(map)));
    }
    //请求更多商品
    public Observable<BaseEntity>requestMoreGoods(HashMap map){
        return convertObservable(HttpFactory.factory(HttpType.SIGNTYPE).getRetrofit()
                .create(ApiService.class).requestLoadHomeGoods(createBody(map)));
    }
    //请求推荐品牌
    private Observable<BaseEntity>requestBrand(){
        return convertObservable(HttpFactory.factory(HttpType.UPLOADTYPE).getRetrofit()
                .create(ApiService.class).requestHomeBrand());
    }
    //请求分类
    private  Observable<CategoryEntity>requestCategory(HashMap map){
        return HttpFactory.factory(HttpType.SIGNTYPE).getRetrofit()
                .create(ApiService.class).requestCategory(createBody(map));
    }
    //获取数据
    public Observable<BaseEntity>requestAll(HashMap map){
        return requestCategory(map).flatMap(
                new Function<CategoryEntity, Observable<BaseEntity>>() {
                    @Override
                    public Observable<BaseEntity> apply(@NonNull CategoryEntity categoryEntity) throws Exception {
                        if (!categoryEntity.status.equals("200")){
                            ErroEntity entity = new ErroEntity();
                            entity.status = "100";
                            entity.message = "分类获取出错";
                            return Observable.just(entity);
                        }
                        if (categoryEntity.data.isEmpty()){
                            ErroEntity entity = new ErroEntity();
                            entity.status = "100";
                            entity.message = "分类获取出错";
                            return Observable.just(entity);
                        }
                        HashMap<String,Object>requestMap = new HashMap<>();
                        requestMap.put("page",1);
                        requestMap.put("typeId",categoryEntity.data.get(0).category_id);
                        return Observable.merge(requestBanner(), requestBrand(),
                                Observable.just(categoryEntity),requestGoods(requestMap));
                    }
                });
    }
}
